package com.chb.docter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chb.docter.dto.PatientAllDto;
import com.chb.docter.mapper.*;
import com.chb.docter.params.PatientQueryParams;
import com.chb.docter.service.PatientService;
import com.hos.entity.his.*;
import com.hos.entity.sys.User;
import com.hos.utils.OssUtils;
import com.hos.utils.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author: 陈海彬
 * @date: Created in 2023/12/12 14:52
 * @description:
 * @modified By:
 * @version:
 */
@Service
public class PatientServiceImpl implements PatientService {

    @Resource
    private PatientMapper patientMapper;
    @Resource
    private PatientFileMapper patientFileMapper;
    @Resource
    private CareHistoryMapper careHistoryMapper;
    @Resource
    private CareOrderMapper careOrderMapper;
    @Resource
    private CareOrderItemMapper careOrderItemMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;


    @Override
    public Patient getPatientInfoByIdCar(String idCar) {
        LambdaQueryWrapper<Patient> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Patient::getIdCard, idCar);
        return patientMapper.selectOne(wrapper);
    }

    @Override
    public Page<Patient> listPatientForPage(PatientQueryParams params) {
        Page<Patient> page = new Page<>(params.getPageNum(), params.getPageSize());
        LambdaQueryWrapper<Patient> wrapper = new LambdaQueryWrapper<>();

        // 进行条件查询
        if (StringUtils.hasText(params.getName())) {
            wrapper.eq(Patient::getName, params.getName());
        }
        if (StringUtils.hasText(params.getIdCard())) {
            wrapper.eq(Patient::getIdCard, params.getIdCard());
        }
        if (StringUtils.hasText(params.getPhone())) {
            wrapper.likeLeft(Patient::getPhone, params.getPhone());
        }
        return this.patientMapper.selectPage(page, wrapper);
    }

    @Override
    public Patient getPatientById(String patientId) {
        return this.patientMapper.selectById(patientId);
    }

    @Override
    public PatientFile getPatientFileById(String patientId) {
        return this.patientFileMapper.selectById(patientId);
    }

    @Override
    public List<CareHistory> getPatientAllMessageByPatientId(String patientId) {
        LambdaQueryWrapper<CareHistory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CareHistory::getPatientId, patientId);
        List<CareHistory> careHistories = this.careHistoryMapper.selectList(wrapper);
        if (careHistories.size() > 0) {
            for (CareHistory careHistory : careHistories) {
                String chId = careHistory.getChId();
                LambdaQueryWrapper<CareOrder> orderLambdaQueryWrapper = new LambdaQueryWrapper<>();
                orderLambdaQueryWrapper.eq(CareOrder::getChId, chId);
                List<CareOrder> orders = this.careOrderMapper.selectList(orderLambdaQueryWrapper);
                careHistory.setCareOrders(orders);
                if (orders.size() > 0) {
                    for (CareOrder order : orders) {
                        String coId = order.getCoId();
                        LambdaQueryWrapper<CareOrderItem> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                        lambdaQueryWrapper.eq(CareOrderItem::getCoId, coId);
                        List<CareOrderItem> careOrderItems = this.careOrderItemMapper.selectList(lambdaQueryWrapper);
                        order.setCareOrderItems(careOrderItems);
                    }
                }
            }
        }
        return careHistories;
    }

    @Override
    public Patient selectPatientByPhone(String phone) {
        LambdaQueryWrapper<Patient> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Patient::getPhone, phone);
        return this.patientMapper.selectOne(wrapper);
    }

    @Override
    public Patient getPatientInfo() {
        return this.getUserByToken();
    }

    @Override
    public String updateAvatar(MultipartFile file) {
        // 1.进行文件上传
        String path = OssUtils.ossUpload(file);
        // 2.获取修改的用户
        Patient user = getUserByToken();
        user.setAvatar(path);
        // 3.进行修改头像
        this.patientMapper.updateById(user);
        return path;
    }

    @Override
    public Patient updatePatientInfo(Patient patient) {
        Patient user = getUserByToken();
        LambdaUpdateWrapper<Patient> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Patient::getPatientId, user.getPatientId());

        wrapper.set(Patient::getName, patient.getName());
        wrapper.set(Patient::getSex, patient.getSex());
        wrapper.set(Patient::getBirthday, patient.getBirthday());
        wrapper.set(Patient::getAddress, patient.getAddress());
        wrapper.set(Patient::getAllergyInfo, patient.getAllergyInfo());
        this.patientMapper.update(null, wrapper);
        return this.patientMapper.selectById(user.getPatientId());
    }

    @Override
    public String upload(MultipartFile file) {
        return OssUtils.ossUpload(file);
    }

    /**
     * @return com.hos.entity.sys.User
     * @create by: 陈海彬
     * @description: 获取请求用户的信息
     * @create time: 2023/12/12 10:39
     */
    private Patient getUserByToken() {
        String token = WebUtil.getToken();
        String phone = redisTemplate.opsForValue().get("user:" + token);
        LambdaQueryWrapper<Patient> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Patient::getPhone, phone);
        Patient patient = this.patientMapper.selectOne(wrapper);
        patient.setPassword("");
        return patient;
    }

}